<%--
  Created by IntelliJ IDEA.
  User: alleon
  Date: Jun 17, 2008
  Time: 8:52:48 PM
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
  <head>
      <title>Server Treeview</title>
      <g:javascript library="yui" />
      
      <yui:javascript dir="yahoo" file="yahoo-min.js" />
      <yui:javascript dir="event" file="event-min.js" />
      <yui:javascript dir="connection" file="connection-min.js" />

      <yui:javascript dir="logger" file="logger-min.js" />
      <yui:stylesheet dir="logger/assets/skins/sam" file="logger.css" />

      <yui:javascript dir="treeview" file="treeview-min.js" />
      <yui:stylesheet dir="treeview/assets/skins/sam" file="treeview.css" />

      <yui:javascript dir="json" file="json-min.js" />
  </head>
  <body class="yui-skin-sam">
%{--     <div id="treePanel">
         <div id="treeDiv"/>
     </div>
     <div id="loggerPanel">
         <div id="loggerDiv"/>
     </div>--}%
     <div id="treeDiv"/>

     <g:javascript>
     (function(){
       //var treePanel = new YAHOO.widget.Panel("treePanel", {close:true, visible:false, draggable:true, constraintoviewport:true});
       //treePanel.show();
       //var loggerPanel = new YAHOO.widget.Panel("loggerPanel", {close:true, visible:false, draggable:true, constraintoviewport:true});
       //loggerPanel.show();
       var myContainer = document.body.appendChild(document.createElement("div"));

       var myLogReader = new YAHOO.widget.LogReader("loggerDiv");

       function buildTree() {
           var tree;
           YAHOO.log("Creating tree");
           tree = new YAHOO.widget.TreeView("treeDiv");

           YAHOO.log("Setting tree for dynamic loading");
           tree.setDynamicLoad(loadNodeData);

           var root = tree.getRoot();

           YAHOO.log("Creating a node");
           var tempNode = new YAHOO.widget.TextNode("/", root, false);

           YAHOO.log("Calling draw");
           tree.draw();
       }

       function loadNodeData(node, fnLoadComplete) {
           YAHOO.log("entering loadNodeData");

           var path = "";
           var leafNode = node;
           while (!leafNode.isRoot()) {
               path = leafNode.label + "/" + path;
               YAHOO.log("path = " + path);
               leafNode = leafNode.parent;
               YAHOO.log("LeafNode has been changed");
           }

           var nodelabel = encodeURI(path);
           YAHOO.log("nodelabel =" + nodelabel);

           var sUrl = "${params.cms}/dir?dir="+nodelabel;

           YAHOO.log("Calling " + sUrl);

           var callback = {
               success: function(oResponse){
                   YAHOO.log("XHR transaction was succesfull.", "info", "example");
                   YAHOO.log(oResponse.responseText);
                   var oResults = YAHOO.lang.JSON.parse(oResponse.responseText);
                   YAHOO.log("length = " + oResults.nodes.length);

                   if ((oResults.nodes)&&(oResults.nodes.length)){
                       YAHOO.log("In the test");
                       if (YAHOO.lang.isArray(oResults.nodes)) {
                           YAHOO.log("This is an array:"+oResults.nodes.length);
                           for (var i=0; i< oResults.nodes.length; i++){
                               var tempNode = new YAHOO.widget.TextNode(oResults.nodes[i].name, node, false);
                               tempNode.isLeaf = !oResults.nodes[i].isDir;
                           }

                       }
                   }
                   oResponse.argument.fnLoadComplete();
               },
               failure: function(oResponse){
                   YAHOO.log("Failed to process XHR transaction.", "info", "example");
                   oResponse.argument.fnLoadComplete();
               },
               argument:{
                   "node": node,
                   "fnLoadComplete":fnLoadComplete
               },
               timeout:5000
           }

           YAHOO.log("Calling ...");
           YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
       };

       YAHOO.util.Event.onDOMReady(buildTree);
         
     }());    
     </g:javascript>
  </body>
</html>
